<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>admin管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>管理<small>admin管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>管理</el-breadcrumb-item>
            <el-breadcrumb-item>admin管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary"  class="butT" @click="formVisible=true;pojo={};checkRole=[];">新增</el-button>
            </div>

            <el-table :data="tableData" border style="width: 100%">
                  <el-table-column prop="admin.id" label="id" width="120"></el-table-column>
                  <el-table-column prop="admin.loginName" label="用户名" width="180"></el-table-column>
                  <el-table-column prop="admin.status" label="用户状态" width="120"></el-table-column>
                  <el-table-column label="角色" width="180">
                            <template slot-scope="scope" >
                                <el-select  placeholder="角色属性"  >
                                    <el-option
                                            v-for="item in scope.row.roleList"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </template>
                    </el-table-column>
                <el-table-column
                            label="操作"  >
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.admin.id)" size="mini" type="primary" size="small">修改</el-button>
                            <el-button @click="dele(scope.row.admin.id)" size="mini" type="danger" size="small">删除</el-button>
                        </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @size-change="fetchData"
                        @current-change="fetchData"
                        :current-page.sync="currentPage"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="size"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <div class="add-form">
                <!--弹出窗口-->
                <el-dialog  title="编辑" :visible.sync="formVisible" >
                    <el-form label-width="80px">
                            <el-form-item label="用户名"><el-input v-model="pojo.loginName"></el-input></el-form-item>
                            <el-form-item label="密码"><el-input  v-model="pojo.password" type="password" ></el-input></el-form-item>
                            <el-form-item label="状态"><el-input v-model="pojo.status"></el-input></el-form-item>


                             <el-form-item label="角色">
                                 <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                                     <div style="margin: 15px 0;"></div>
                                     <el-checkbox-group v-model="checkRole" @change="handleCheckedCitiesChange">
                                     <el-checkbox v-for="item in roleList" :label="item" :key="item.id">{{item.name}}</el-checkbox>
                                 </el-checkbox-group>
                             </el-input></el-form-item>


                            <el-form-item>
                                <el-button type="primary" @click="save()">保存</el-button>
                                <el-button @click="formVisible = false" >关闭</el-button>
                            </el-form-item>
                    </el-form>
                </el-dialog>
            </div>

        </div>
    </div>


</div>
</body>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script>
     new Vue({
        el: '#app',
        data(){
            return {
                tableData: [],
                currentPage: 1,
                total: 10,
                size: 10,
                searchMap: {},
                pojo: {},
                formVisible: false,
                checkRole:[],
                roleList:[],
                checkAll: false,
                isIndeterminate: true,
                wholeAdminRole:{},
                isChange:false
            }
        },
        created(){
            this.fetchData();
            /*发送ajax请求获取所有角色封装进数组*/
            axios.get(`/role/findAll.do`).then(response=>{
                this.roleList=response.data;
            });
        },
        methods:{
            fetchData (){
                /*发送ajax请求，响应的对象[{ 用户对象：Admin，角色集合：List<Role> }]*/
                axios.get(`/admin/findWholeAdmin.do`).then(response => {
                    this.tableData = response.data;
                });
            },
            save (){

                this.wholeAdminRole.admin=this.pojo;
                this.wholeAdminRole.roleList=this.checkRole;
                //转为json格式
                //let newWholeAdminRole=JSON.stringify(this.wholeAdminRole)
                //this.$alert(newWholeAdminRole)
                axios.post(`/admin/${this.pojo.id==null?'addWholeAdmin':'updateWholeAdmin'}.do`, this.wholeAdminRole).then(response => {
                    if(response.data.code==1){
                        this.$alert(response.data.message);
                    }
                    this.fetchData (); //刷新列表
                    this.formVisible = false ;//关闭窗口
                });
            },
            edit (id){
                this.formVisible = true // 打开窗口
                this.isChange=true; //密码不可输入
                axios.get(`/role/findAll.do`).then(response=>{
                    this.roleList=response.data;
                });
                // 调用查询
                axios.get(`/admin/findWholeAdminById.do?id=${id}`).then(response => {
                    this.pojo = response.data.admin;
                    //this.roleList=response.data.roleList;
                })
            },
            dele (id){
                this.$confirm('确定要删除此记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then( () => {
                    axios.get(`/admin/deleteWholeAdmin.do?id=${id}`).then(response => {
                        this.fetchData (); //刷新列表
                    })
                })
            },
            handleCheckAllChange(val) {
                this.checkedCities = val ? roleList : [];
                this.isIndeterminate = false;
            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.roleList.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.roleList.length;
            }

        }
    })
</script>
</html>
